﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Youz.Common.Helper;
using Youz.CreditRepay.BLL;
using Youz.CreditRepay.Entity;

namespace Youz.CreditRepay.Server.Pay
{
    /// <summary>
    /// notify 的摘要说明
    /// </summary>
    public class notify : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            var bll = new Pay_RecordsBLL();
            var Request = context.Request;
            var Response = context.Response;

            //数字签名验证密钥，请与
            string key = "111";

            ///↓↓↓↓↓↓↓↓↓提交过来的参数↓↓↓↓↓↓↓↓↓↓↓↓↓↓

            string payType = WebHelper.Post("PayType");//交易号
            string tradeNo = WebHelper.Post("No");//交易号
            string time = WebHelper.Post("Time");//付款时间
            string title = WebHelper.Post("Title");//支付说明
            string payer = WebHelper.Post("Payer");//对方用户名
            string payerID = WebHelper.Post("PayerID");//对方用户ID
            string amount = WebHelper.Post("Amount");//金额
            string dsv = WebHelper.Post("dsv");//数字签名验证


            //↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

            //核对签名
            string str = string.Join("|", tradeNo, time, title, payer, payerID, amount, key, payType);
            if (!string.IsNullOrWhiteSpace(dsv) && System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5").Equals(dsv, StringComparison.InvariantCultureIgnoreCase))
            {
                bll.Add(new Pay_Records
                {
                    PayType = (PaymentType)int.Parse(payType),
                    TradeNo = tradeNo,
                    TradeTime = TypeHelper.ConvertTo<DateTime>(time),
                    TradeName = title,
                    Payer = payer,
                    Amount = decimal.Parse(amount),
                    Status = 0
                });

                Response.Write("1");
                Response.End();
            }
            else
            {
                Response.Write("2");
                Response.End();
            }
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}